/*
 * Created by SharpDevelop.
 * User: Tivadar Papai
 * Date: 8/27/2007
 * Time: 3:13 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;

namespace RoverStudio.Math
{
	/// <summary>
	/// Description of Vector2D.
	/// </summary>
	public class Vector2D
	{
        private double x;

        public double X
        {
            get { return x; }
            set { x = value; }
        }

        private double y;

        public double Y
        {
            get { return y; }
            set { y = value; }
        }

        public Vector2D() : this(0, 0) { }

        public Vector2D(double x, double y)
        {
            this.x = x;
            this.y = y;
        }

        public static double operator ^(Vector2D operand1, Vector2D operand2)
        {
            return operand1.x * operand2.y - operand2.x * operand1.y;
        }

        public static double operator *(Vector2D operand1, Vector2D operand2)
        {
            return operand1.x * operand2.x + operand1.y * operand2.y;
        }

        public static Vector2D operator *(Vector2D operand1, double operand2)
        {
            return new Vector2D(operand1.x * operand2, operand1.y * operand2);
        }

        public static Vector2D operator /(Vector2D operand1, double operand2)
        {
            return new Vector2D(operand1.x / operand2, operand1.y / operand2);
        }

        public static Vector2D operator *(double operand1, Vector2D operand2)
        {
            return new Vector2D(operand1 * operand2.x, operand1 * operand2.y);
        }

        public static Vector2D operator +(Vector2D operand1, Vector2D operand2)
        {
            return new Vector2D(operand1.x + operand2.x, operand1.y + operand2.y);
        }

        public static Vector2D operator -(Vector2D operand1, Vector2D operand2)
        {
            return new Vector2D(operand1.x - operand2.x, operand1.y - operand2.y);
        }

        public Vector2D Scale(double scale)
        {
            this.x *= scale;
            this.y *= scale;
            return this;
        }

        public Vector2D Shift(Vector2D shift)
        {
            this.x += shift.x;
            this.y += shift.y;
            return this;

        }

        public Vector2D FlipVertical()
        {
            return new Vector2D(-this.x, this.y);
        }

        public Vector2D FlipHorizontal()
        {
            return new Vector2D(this.x, -this.y);
        }

        public double Length()
        {
            return System.Math.Sqrt(x * x + y * y);
        }

        public Vector2D GetNormalizedVector2D()
        {
            double length = this.Length();
            return new Vector2D(this.x / length, this.y / length);
        }

        public static implicit operator Vector3D(Vector2D vector)
        {
            return new Vector3D(vector.x, vector.y, 0.0);
        }

        public static Vector2D operator -(Vector2D vector)
        {
            return (-1) * vector;
        }

        public override string ToString()
        {
            return String.Format("({0:00.00}; {1:00.00})", x, y);
        }
	}
}
